/*
package com.diana.serviceImpl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.diana.common.PageUtils;
import com.diana.common.ScheduleUtils;
import com.diana.common.StatusEnums;
import com.diana.common.Utils;
import com.diana.entity.ScheduleJob;
import com.diana.mapper.ScheduleJobMapper;
import com.diana.service.ScheduleJobService;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

*/
/**
 * <p>
 * 定时任务 服务实现类
 * </p>
 *
 * @author anzhi
 * @since 2019-11-11
 *//*

@Service("scheduleJobService")
public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobMapper, ScheduleJob> implements ScheduleJobService {

    @Autowired
    private Scheduler scheduler;

    */
/**
     * 项目启动时，初始化定时器
     *//*

    @PostConstruct
    public void init(){
        List<ScheduleJob> scheduleJobList = this.selectList(Condition.create());
        for(ScheduleJob scheduleJob : scheduleJobList){
            CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getJobId());
            //如果不存在，则创建
            if(cronTrigger == null) {
                ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
            }else {
                ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
            }
        }
    }

    */
/**
     * 新增job
     * @param scheduleJob
     * @return
     *//*

    @Override
    @Transactional
    public String add(ScheduleJob scheduleJob) {
        if(Utils.isNullOrEmpty(scheduleJob.getBeanName())){
            return Utils.message("0001","请输入Job的SpringBean名称",null);
        }
        if(Utils.isNullOrEmpty(scheduleJob.getCronExpression())){
            return Utils.message("0001","请输入Cron表达式",null);
        }
        ScheduleJob job = this.selectOne(Condition.create().eq("beanName", scheduleJob.getBeanName()).ne("status", StatusEnums.JobStatus.删除));
        if(job!=null){
            return Utils.message("0001","该SpringBean名称已存在",null);
        }
        scheduleJob.setStatus(StatusEnums.JobStatus.启用);
        boolean insert = this.insert(scheduleJob);
        boolean createJob = ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
        if(insert&&createJob){
            return Utils.message("0000","操作成功",null);
        }
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return Utils.message("0001","操作失败",null);
    }

    */
/**
     * 更新任务
     * @param scheduleJob
     * @return
     *//*

    @Override
    @Transactional
    public String update(ScheduleJob scheduleJob) {
        ScheduleJob job = this.selectOne(Condition.create().eq("beanName", scheduleJob.getBeanName()).ne("status", StatusEnums.JobStatus.删除).ne("jobId",scheduleJob.getJobId()));
        if(job!=null){
            return Utils.message("0001","该SpringBean名称已存在",null);
        }
        boolean update = this.updateById(scheduleJob);
        boolean updateScheduleJob = ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
        if(update&&updateScheduleJob){
            return Utils.message("0000","操作成功",null);
        }
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return Utils.message("0001","操作失败",null);
    }

    @Override
    public String getInfo(Long jobId) {
        return null;
    }

    */
/**
     * 获取列表
     * @param map
     * @return
     *//*

    @Override
    public String getList(Map map) {
        Integer index = Utils.toInteger(map.get("index"));
        Integer size = Utils.toInteger(map.get("size"));
        Page<ScheduleJob> page = PageUtils.getPage(index, size);
        Page<ScheduleJob> jobPage = this.selectPage(page);
        return Utils.message("0000","获取成功",jobPage.getRecords(),jobPage.getTotal());
    }

    */
/**
     * 执行任务
     * @param jobId
     * @return
     *//*

    @Override
    public String run(Long jobId) {
        ScheduleJob scheduleJob = this.selectOne(Condition.create().ne("status",StatusEnums.JobStatus.删除));
        if(scheduleJob==null){
            return Utils.message("0001","Job不存在",null);
        }
        boolean run = ScheduleUtils.run(scheduler, scheduleJob);
        if(run){
            return Utils.message("0000","已开始执行任务",null);
        }
        return Utils.message("0001","执行任务失败",null);
    }

    */
/**
     * 删除job
     * @param jobId
     * @return
     *//*

    @Override
    @Transactional
    public String deleteJob(Long jobId) {
        ScheduleJob scheduleJob = this.selectOne(Condition.create().ne("status",StatusEnums.JobStatus.删除));
        if(scheduleJob==null){
            return Utils.message("0001","Job不存在",null);
        }
        boolean deleteById = this.deleteById(jobId);
        boolean delete = ScheduleUtils.deleteScheduleJob(scheduler, jobId);
        if(deleteById&&delete){
            return Utils.message("0000","操作成功",null);
        }
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return Utils.message("0001","操作失败",null);
    }

    */
/**
     * 暂停任务
     * @param jobId
     * @return
     *//*

    @Override
    public String pauseJob(Long jobId) {
        ScheduleJob scheduleJob = this.selectOne(Condition.create().eq("status",StatusEnums.JobStatus.启用));
        if(scheduleJob==null){
            return Utils.message("0001","Job不存在,或已暂停",null);
        }
        boolean pauseJob = ScheduleUtils.pauseJob(scheduler, jobId);
        scheduleJob = new ScheduleJob();
        scheduleJob.setJobId(jobId);
        scheduleJob.setStatus(StatusEnums.JobStatus.暂停);
        boolean b = this.updateById(scheduleJob);
        if(pauseJob){
            return Utils.message("0000","操作成功",null);
        }
        return Utils.message("0001","操作失败",null);
    }

    */
/**
     * 恢复任务
     * @param jobId
     * @return
     *//*

    @Override
    public String resumeJob(Long jobId) {
        ScheduleJob scheduleJob = this.selectOne(Condition.create().eq("status",StatusEnums.JobStatus.暂停));
        if(scheduleJob==null){
            return Utils.message("0001","Job不存在,或已启用",null);
        }
        boolean resumeJob = ScheduleUtils.resumeJob(scheduler, jobId);
        scheduleJob = new ScheduleJob();
        scheduleJob.setJobId(jobId);
        scheduleJob.setStatus(StatusEnums.JobStatus.启用);
        boolean b = this.updateById(scheduleJob);
        if(resumeJob){
            return Utils.message("0000","操作成功",null);
        }
        return Utils.message("0001","操作失败",null);
    }
}
*/
